package com.union.core.workModel.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.hnjowe.webdb.DBOperation;
import com.union.core.workModel.dao.ModelMemberDao;
import com.union.core.workModel.model.ModelMember;

public class ModelMemberDaoImpl implements ModelMemberDao{
	private DBOperation opera;
	private ModelMember modelMember;
	private static Logger log = Logger.getLogger(ModelInfoDaoImpl.class.getName());
	@Override
	public boolean addMember(ModelMember modelMember) {
		boolean result = false;
		try {
			opera = new DBOperation();
			opera.transcationBegin();
			String sql = "insert into model_family_member(" +
					"member_name,model_id,member_sex,member_birth_date," +
					"member_relation,member_job,mebmer_income) values " +
					"(?,?,?,?,?,?,?)";
			opera.prepStatment(sql);
			opera.setString(1, modelMember.getMember_name());
			opera.setInt(2, modelMember.getModel_id());
			opera.setInt(3, modelMember.getMember_sex());
			opera.setString(4, modelMember.getMember_birth_date());
			opera.setString(5, modelMember.getMember_relation());
			opera.setString(6, modelMember.getMember_job());
			opera.setFloat(7, modelMember.getMebmer_income());
			opera.execute();
			result = opera.transcationCommit();
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return result;
	}
	@Override
	public boolean delMember(int memberId) {
		boolean result = false;
		try {
			opera = new DBOperation();
			opera.transcationBegin();
			String sql = "delete from model_family_member where member_id=?";
			opera.prepStatment(sql);
			opera.setInt(1, memberId);
			opera.execute();
			result = opera.transcationCommit();
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return result;
	}
	@Override
	public boolean editMember(ModelMember modelMember) {
		boolean result = false;
		try {
			opera = new DBOperation();
			opera.transcationBegin();
			String sql = "update model_family_member set member_name=?,member_sex=?," +
					"member_birth_date=?,member_relation=?,member_job=?," +
					"mebmer_income=? where member_id=?";
			opera.prepStatment(sql);
			opera.setString(1, modelMember.getMember_name());
			opera.setInt(2, modelMember.getMember_sex());
			opera.setString(3, modelMember.getMember_birth_date());
			opera.setString(4, modelMember.getMember_relation());
			opera.setString(5, modelMember.getMember_job());
			opera.setFloat(6, modelMember.getMebmer_income());
			opera.executeUpdate();
			result = opera.transcationCommit();
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return result;
	}
	@Override
	public ModelMember queryById(int memberId) {
		ModelMember modelMember = null;
		try {
			opera = new DBOperation();
			opera.transcationBegin();

			String sql = "select member_id,member_name,model_id," +
					"member_sex,member_birth_date,member_relation," +
					"member_job,mebmer_income "
					+ "from model_family_member where  member_id=?";
			opera.prepStatment(sql);
			opera.setInt(1, memberId);
			ResultSet res = opera.executeQuery();
			if (res.next()) {
				modelMember = new ModelMember();
				modelMember.setMember_id(res.getInt(1));
				modelMember.setMember_name(res.getString(2));
				modelMember.setModel_id(res.getInt(3));
				modelMember.setMember_sex(res.getInt(4));
				modelMember.setMember_birth_date(res.getString(5));
				modelMember.setMember_relation(res.getString(6));
				modelMember.setMember_job(res.getString(7));
				modelMember.setMebmer_income(res.getFloat(8));
			}
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return modelMember;
	}
	@Override
	public List<ModelMember> queryMemberList(int modelId) {
		List<ModelMember> memberList = new ArrayList<ModelMember>();
		try{
			opera = new DBOperation();
			opera.transcationBegin();
			String sql = "select "
					+ "member_id,member_name,model_id," +
					"member_sex,member_birth_date,member_relation," +
					"member_job,mebmer_income "
					+ "from model_family_member "
					+ "where model_id=?";
			opera.prepStatment(sql);
			opera.setInt(1, modelId);
			ResultSet res = opera.executeQuery();// 执行上面语句
			int i = 0;
			
			while (res.next()) {
				modelMember = new ModelMember();
				modelMember.setMember_id(res.getInt(1));
				modelMember.setMember_name(res.getString(2));
				modelMember.setModel_id(res.getInt(3));
				modelMember.setMember_sex(res.getInt(4));
				modelMember.setMember_birth_date(res.getString(5));
				modelMember.setMember_relation(res.getString(6));
				modelMember.setMember_job(res.getString(7));
				modelMember.setMebmer_income(res.getFloat(8));
				memberList.add(i, modelMember);
				modelMember=null;
				i++;
			}
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return memberList;
	}
	@Override
	public boolean delMemberList(int modelId) {
		boolean result = false;
		try {
			opera = new DBOperation();
			opera.transcationBegin();
			String sql = "delete from model_family_member where model_id=?";
			opera.prepStatment(sql);
			opera.setInt(1, modelId);
			opera.execute();
			result = opera.transcationCommit();
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return result;
	}
	
	
}
